package com.lframework.xingyun.basedata.bo.product.spec;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.lframework.starter.common.utils.BeanUtil;
import com.lframework.starter.web.core.bo.BaseBo;
import com.lframework.starter.web.core.utils.ApplicationUtil;
import com.lframework.xingyun.basedata.bo.product.info.ProductInfoBo;
import com.lframework.xingyun.basedata.bo.product.speccost.GetProductSpecCostDetailBo;
import com.lframework.xingyun.basedata.entity.ProductSpec;
import com.lframework.xingyun.basedata.entity.ProductSpecCostDetail;
import com.lframework.xingyun.basedata.service.product.ProductService;
import com.lframework.xingyun.basedata.service.product.ProductSpecCostDetailService;
import com.lframework.xingyun.basedata.vo.product.speccost.QueryProductSpecCostDetailVo;
import io.swagger.annotations.ApiModelProperty;

import lombok.Data;

/**
 * <p>
 * 商品规格表 QueryBo
 * </p>
 *
 */
@Data
public class QueryProductSpecBo extends BaseBo<ProductSpec> {

    /**
     * ID
     */
    @ApiModelProperty("ID")
    private String id;

    /**
     * 主商品id
     */
    @ApiModelProperty("主商品id")
    private String productId;

    /**
     * 属性id
     */
    @ApiModelProperty("属性id")
    private String propertyId;

    /**
     * 属系值id
     */
    @ApiModelProperty("属系值id")
    private String itemId;

    /**
     * 采购价
     */
    @ApiModelProperty("采购价")
    private BigDecimal purchasePrice;

    /**
     * 零售价
     */
    @ApiModelProperty("零售价")
    private BigDecimal retailPrice;

    /**
     * 销售价
     */
    @ApiModelProperty("销售价")
    private BigDecimal salePrice;

    /**
     * 是否可用
     */
    @ApiModelProperty("是否可用")
    private Boolean available;

    /**
     * 成本详情
     */
    @ApiModelProperty(value = "成本详情")
    private List<GetProductSpecCostDetailBo> costDetailData;

    public QueryProductSpecBo() {

    }

    public QueryProductSpecBo(ProductSpec dto) {

        super(dto);
    }

    @Override
    public BaseBo<ProductSpec> convert(ProductSpec dto) {
        return super.convert(dto);
    }

    @Override
    protected void afterInit(ProductSpec dto) {
        this.costDetailData = new ArrayList<>();
        ProductSpecCostDetailService specCostDetailService = ApplicationUtil.getBean(ProductSpecCostDetailService.class);
        ProductService productService = ApplicationUtil.getBean(ProductService.class);
        QueryProductSpecCostDetailVo queryVo = new QueryProductSpecCostDetailVo();
        queryVo.setMainProductId(dto.getProductId());
        queryVo.setPropertyId(dto.getPropertyId());
        queryVo.setItemId(dto.getItemId());
        // 查询规格成分商品列表
        List<ProductSpecCostDetail> productSpecCostDetails = specCostDetailService.query(queryVo);
        if (CollUtil.isNotEmpty(productSpecCostDetails)) {
            // 成分商品id列表
            List<String> productIds = productSpecCostDetails.stream().map(ProductSpecCostDetail::getProductId).collect(Collectors.toList());
            // 查询成分商品详情
            List<ProductInfoBo> productInfoBos = productService.selectByBo(productIds);
            List<GetProductSpecCostDetailBo> costDetailData = new ArrayList<>();
            productSpecCostDetails.stream()
                .filter(item -> item.getType() == 0)
                .forEach(productSpecCostDetail -> {
                    GetProductSpecCostDetailBo getProductSpecCostDetailBo = BeanUtil.copyProperties(productSpecCostDetail, GetProductSpecCostDetailBo.class);
                    ProductInfoBo productInfoBo = getByProductId(productInfoBos, productSpecCostDetail.getProductId());
                    if (Objects.nonNull(productInfoBo)) {
                        getProductSpecCostDetailBo.setProductCode(productInfoBo.getProductCode());
                        getProductSpecCostDetailBo.setProductName(productInfoBo.getProductName());
                        getProductSpecCostDetailBo.setCategoryName(productInfoBo.getCategoryName());
                        getProductSpecCostDetailBo.setBrandName(productInfoBo.getBrandName());
                        getProductSpecCostDetailBo.setSkuCode(productInfoBo.getSkuCode());
                        getProductSpecCostDetailBo.setUnit(productInfoBo.getUnit());
                        getProductSpecCostDetailBo.setSalePrice(productInfoBo.getSalePrice());
                        costDetailData.add(getProductSpecCostDetailBo);
                    }
                });
            productSpecCostDetails.stream()
                .filter(item -> item.getType() == 1)
                .forEach(productSpecCostDetail -> {
                    GetProductSpecCostDetailBo getProductSpecCostDetailBo = getProductSpecCostDetailBoBySourceProductId(costDetailData, productSpecCostDetail.getSourceProductId());
                    if (Objects.nonNull(getProductSpecCostDetailBo)) {
                        ProductInfoBo productInfoBo = getByProductId(productInfoBos, productSpecCostDetail.getProductId());
                        if (Objects.nonNull(productInfoBo)) {
                            GetProductSpecCostDetailBo bo = BeanUtil.copyProperties(productSpecCostDetail, GetProductSpecCostDetailBo.class);
                            bo.setProductCode(productInfoBo.getProductCode());
                            bo.setProductName(productInfoBo.getProductName());
                            bo.setCategoryName(productInfoBo.getCategoryName());
                            bo.setBrandName(productInfoBo.getBrandName());
                            bo.setSkuCode(productInfoBo.getSkuCode());
                            bo.setUnit(productInfoBo.getUnit());
                            bo.setSalePrice(productInfoBo.getSalePrice());
                            getProductSpecCostDetailBo.addReplacement(bo);
                        }
                    }
                });
            this.costDetailData = costDetailData;
        }
    }

    private ProductInfoBo getByProductId(List<ProductInfoBo> productInfoBos, String productId) {
        for (ProductInfoBo productInfoBo : productInfoBos) {
            if (productInfoBo.getProductId().equals(productId)) {
                return productInfoBo;
            }
        }
        return null;
    }

    private GetProductSpecCostDetailBo getProductSpecCostDetailBoBySourceProductId(List<GetProductSpecCostDetailBo> costDetailData, String sourceProductId) {
        for (GetProductSpecCostDetailBo getProductSpecCostDetailBo : costDetailData) {
            if (getProductSpecCostDetailBo.getProductId().equals(sourceProductId)) {
                return getProductSpecCostDetailBo;
            }
        }
        return null;
    }
}

